<template>
  <div>
    <el-form ref="myform" :model="formItem" :rules="rules" label-width="120px" size="mini">
      <el-form-item label="供应商名称" prop="name">
        <el-input v-model="formItem.name" placeholder="请输入供应商名称" />
      </el-form-item>
      <el-form-item label="供应商地址" prop="address">
        <el-input v-model="formItem.address" placeholder="请输入供应商地址" />
      </el-form-item>
      <el-form-item label="供应商联系人" prop="person">
        <el-input v-model="formItem.person" placeholder="请输入供应商联系人" />
      </el-form-item>
      <el-form-item label="供应商联系电话" prop="phone">
        <el-input v-model="formItem.phone" placeholder="请输入供应商联系电话" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  create,
  update,
  detail,
  checkName
} from '@neu/dcam-ui/api/manufacturer'

// 定义一个表单原始项
const formItem = {
  name: '',
  person: '',
  phone: '',
  address: '',
  bank: '',
  accountNumber: '',
  taxId: '',
  postCode: '',
  status: true
}
export default {
  name: 'ManufacturerEditForm',
  props: {
    // 根据id可判断是新建还是更新
    id: {
      required: false,
      type: Number,
      default: -1
    }
  },
  data() {
    const validateDuplicateName = (rule, value, callback) => {
      if (this.id === -1) {
        checkName({ name: value }).then(response => {
          if (response.data) {
            callback(new Error('供应商名称已存在'))
          } else {
            callback()
          }
        })
      } else {
        callback()
      }
    }
    return {
      // 编辑的表单内容
      formItem: {
        ...formItem
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, trigger: 'change', validator: validateDuplicateName },
          { required: true, message: '供应商名称不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        address: [
          // { required: true, message: '供应商地址不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        person: [
          // { required: true, message: '供应商联系人不能为空', trigger: 'blur' },
          { max: 225, message: '长度不能超过225', trigger: 'blur' }
        ],
        phone: [
          {
            validator: function(rule, value, callback) {
              if (!(/^1(3|4|5|7|8)\d{9}$/.test(value) === true || /^0\d{2,3}-[1-9]\d{6,7}$/.test(value) === true)) {
                callback(new Error('请输入正确的联系方式'))
              } else {
                callback()
              }
            },
            trigger: 'blur'
          }
        ]
      },
      modal: true
    }
  },
  watch: {
    // 监控id的变化，初始化表单数据
    id: function() {
      this.fetch()
    }
  },
  created() {
    this.fetch()
  },
  methods: {
    // 初始化表单数据
    fetch() {
      if (this.id !== -1) {
        detail(this.id).then(response => {
          this.formItem = {
            ...response.data
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const updated = this.id !== -1
      return new Promise(resolve => {
        this.$refs.myform.validate((valid) => {
          if (valid) {
            if (updated) {
              resolve(update(this.id, this.formItem))
            } else {
              resolve(create(this.formItem))
            }
          } else {
            resolve(false)
          }
        })
      })
    },
    reset() {
      this.$refs.myform.resetFields()
    }
  }
}
</script>
